package com.chinaisafe.user.controller;

import com.chinaisafe.base.common.validated.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.chinaisafe.base.common.annotation.OperationLog;
import com.chinaisafe.base.common.enums.BusinessTypeEnum;
import com.chinaisafe.base.common.validated.AddGroup;
import com.chinaisafe.base.common.vo.Answer;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.chinaisafe.user.vo.request.SysDeptReqVO;
import com.chinaisafe.user.vo.response.SysDeptRespVO;
import org.springframework.validation.annotation.Validated;
import com.chinaisafe.user.service.SysDeptService;
import org.springframework.web.bind.annotation.RestController;

/**
 * SysDeptController
 * Description：部门接口层
 *
 * @author xuefu.shu
 * @date  2024-07-12 10:39:32
 */
@RestController
@RequestMapping("/sysDept")
@Tag(name = "部门接口层", description = "部门接口层")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping("/pageList")
    @Operation(summary = "获取部门页列表", description = "获取部门分页列表")
    public Answer<IPage<SysDeptRespVO>> pageList(SysDeptReqVO sysDeptReqVO) {
        IPage<SysDeptRespVO> pageList = sysDeptService.getPageList(sysDeptReqVO);
        return Answer.success("获取成功", pageList);
    }

    @PostMapping()
    @Operation(summary = "新增部门", description = "新增部门")
    @OperationLog(title = "部门-新增", businessType = BusinessTypeEnum.INSERT)
    public Answer<SysDeptRespVO> add(@RequestBody @Validated(AddGroup.class) SysDeptReqVO sysDeptReqVO) {
        SysDeptRespVO sysDeptRespVO = sysDeptService.add(sysDeptReqVO);
        return Answer.success("保存成功", sysDeptRespVO);
    }

    @PutMapping()
    @Operation(summary = "根据id编辑部门", description = "根据id编辑部门")
    @OperationLog(title = "部门-根据id编辑", businessType = BusinessTypeEnum.UPDATE)
    public Answer<SysDeptRespVO> update(@RequestBody @Validated(UpdateGroup.class) SysDeptReqVO sysDeptReqVO) {
        SysDeptRespVO sysDeptRespVO = sysDeptService.update(sysDeptReqVO);
        return Answer.success("修改成功", sysDeptRespVO);
    }

    @GetMapping("/{sysDeptId}")
    @Operation(summary = "根据id获取部门", description = "根据id获取部门")
    public Answer<SysDeptRespVO> getDetailById(@PathVariable Long sysDeptId) {
        SysDeptRespVO sysDeptRespVO = sysDeptService.getDetailById(sysDeptId);
        return Answer.success("获取成功", sysDeptRespVO);
    }

    @DeleteMapping()
    @Operation(summary = "根据id删除部门", description = "根据id删除部门")
    @OperationLog(title = "部门-根据id删除", businessType = BusinessTypeEnum.DELETE)
    public Answer<Void> deleteByIds(@RequestParam("ids") Long[] ids) {
        sysDeptService.deleteByIds(ids);
        return Answer.success("删除成功");
    }
}

